<%--
  Created by IntelliJ IDEA.
  Author: WaveHi
  Contact: HJBWaveHi@163.com
  Date: 25/9/2021
  Time: 上午 9:54
  Description: Navigation of page.
  Version: 1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<c:if test="${requestScope.page.dataTotalCount ne 0}">
    <c:if test="${requestScope.page.pageNo gt 1}">
        <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=1">First page</a>
        <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=${requestScope.page.pageNo - 1}">Previous page</a>
    </c:if>
    <c:choose>
        <c:when test="${requestScope.page.pageTotalCount lt 5}">
            <c:set var="begin" value="1"></c:set>
            <c:set var="end" value="${requestScope.page.pageTotalCount}"></c:set>
        </c:when>
        <c:when test="${requestScope.page.pageTotalCount ge 5}">
            <c:choose>
                <%-- 前5个 --%>
                <c:when test="${requestScope.page.pageNo le 3}">
                    <c:set var="begin" value="1"></c:set>
                    <c:set var="end" value="5"></c:set>
                </c:when>
                <%-- 后5个 --%>
                <c:when test="${requestScope.page.pageNo ge requestScope.page.pageTotalCount - 2}">
                    <c:set var="begin" value="${requestScope.page.pageTotalCount - 4}"></c:set>
                    <c:set var="end" value="${requestScope.page.pageTotalCount}"></c:set>
                </c:when>
                <%-- 中间5个 --%>
                <c:otherwise>
                    <c:set var="begin" value="${requestScope.page.pageNo - 2}"></c:set>
                    <c:set var="end" value="${requestScope.page.pageNo + 2}"></c:set>
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>
    <c:forEach begin="${begin}" end="${end}" var="data">
        <c:if test="${data eq requestScope.page.pageNo}">
            <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=${data}">[${data}]</a>
        </c:if>
        <c:if test="${data ne requestScope.page.pageNo}">
            <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=${data}">${data}</a>
        </c:if>
    </c:forEach>
    <c:if test="${requestScope.page.pageNo < requestScope.page.pageTotalCount}">
        <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=${requestScope.page.pageNo + 1}">Next page</a>
        <a href="${requestScope.page.url}&pageSize=${requestScope.page.pageSize}&pageNo=${requestScope.page.pageTotalCount}">Last page</a>
    </c:if>
    <span>共${requestScope.page.dataTotalCount}数据</span>
    <span>每页<input id="page_pageSize" type="text" name="pageSize" value="${requestScope.page.pageSize}"/> 条数据</span>
    <span>共${requestScope.page.pageTotalCount}页</span>
    <span>跳转到</span><input type="text" id="page_pageNo"/><span>页</span>
    <button id="nav_btn">跳转</button>
</c:if>
<script type="text/javascript">
    $(function () {
        $("#page_pageSize").blur(
            function () {
                //获取目标元素的值
                let pageSize = $("#page_pageSize").val();
                //跳转
                location.href = "${pageScope.basePath}${requestScope.page.url}&pageSize="  + pageSize;
            }
        )
        $("#nav_btn").click(
            function () {
                //获取需要跳转的页数
                let pageNo = $("#page_pageNo").val();
                //跳转
                location.href="${pageScope.basePath}" + "${requestScope.page.url}&pageSize=${requestScope.page.pageSize}" + "&pageNo=" + pageNo;
            }
        );
    });
</script>
